package com.xianshiyue;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;


public class Customer {

    Logger log = Logger.getLogger(Customer.class);
    public boolean loop = true;
    StringBuilder msg = new StringBuilder();// 输出到控制台的信息
    public int error;

    /**
     * 上次短信通知时间
     */
    private long lastTime_hand2, lastTime_max, lastTime_mi5, lastTime_fly,
            lastTime_ledLight;
    /**
     * 短信通知总数量
     */
    private int count_hand2, count_max, count_mi5, count_fly, count_ledLight;

    /**
     * 小米无人机
     */
    public void findFly() throws JSONException {
        java.util.Map<String, String> map = new java.util.HashMap<>();
        map.put("client_id", "180100031051");
        map.put("product_id", "3753");
        map.put("width", "720");
        JSONObject json = find(map);
        if (json == null) {
            return;
        }
        long now = System.currentTimeMillis();
        JSONObject data = json.getJSONObject("data");
        log.info(data.getString("name"));
        JSONArray color = data.getJSONArray("commodity_list");
        for (int i = 0; i < color.length(); i++) {
            msg.setLength(0);
            JSONObject c = color.getJSONObject(i);
            if (c.getBoolean("is_cos")) {
                msg.append(c.getString("icon_name"));
                msg.append("-->没货");
            } else {
                msg.append(data.getString("name"));
                msg.append(c.getString("icon_name"));
                msg.append("-->有货！！！  ");
                // 离上次通知大于3分钟就短信通知
                if (now - lastTime_fly > 3L * 60 * 1000 && count_fly < 3) {
                    lastTime_fly = now;
                    count_fly++;
                    send(msg.toString());
                }
            }
            log.info(msg.toString());
        }
        log.info("================================================");
    }

    /**
     * 手环2
     */
    public void findHand2() {
        java.util.Map<String, String> map = new java.util.HashMap<>();
        map.put("client_id", "180100031051");
        map.put("product_id", "3814");
        map.put("width", "720");
        JSONObject json = find(map);
        if (json == null) {
            return;
        }
        long now = System.currentTimeMillis();
        JSONObject data = json.getJSONObject("data");
        log.info(data.getString("name"));
        JSONArray color = data.getJSONArray("commodity_list");
        for (int i = 0; i < color.length(); i++) {
            msg.setLength(0);
            JSONObject c = color.getJSONObject(i);
            if (c.getBoolean("is_cos")) {
                msg.append(c.getString("icon_name"));
                msg.append("-->没货");
            } else {
                msg.append(data.getString("name"));
                msg.append(c.getString("icon_name"));
                msg.append("-->有货！！！  ");
                // 离上次通知大于3分钟就短信通知
                if (now - lastTime_hand2 > 3L * 60 * 1000 && count_hand2 < 3) {
                    lastTime_hand2 = now;
                    count_hand2++;
                    send(msg.toString());
                }
            }
            log.info(msg.toString());
        }
        log.info("================================================");
    }

    /**
     * 米家LED台灯
     */
    public void findLEDlight() {
        java.util.Map<String, String> map = new java.util.HashMap<>();
        map.put("client_id", "180100031051");
        map.put("product_id", "3806");
        map.put("width", "720");
        JSONObject json = find(map);
        if (json == null) {
            return;
        }
        long now = System.currentTimeMillis();
        JSONObject data = json.getJSONObject("data");
        log.info(data.getString("name"));
        JSONArray color = data.getJSONArray("commodity_list");
        for (int i = 0; i < color.length(); i++) {
            msg.setLength(0);
            JSONObject c = color.getJSONObject(i);
            if (c.getBoolean("is_cos")) {
                msg.append(c.getString("icon_name"));
                msg.append("-->没货");
            } else {
                msg.append(data.getString("name"));
                msg.append(c.getString("icon_name"));
                msg.append("-->有货！！！  ");
                // 离上次通知大于3分钟就短信通知
                if (now - lastTime_ledLight > 3L * 60 * 1000 && count_ledLight < 3) {
                    lastTime_ledLight = now;
                    count_ledLight++;
                    send(msg.toString());
                }
            }
            log.info(msg.toString());
        }
        log.info("================================================");
    }

    /**
     * 小米max
     */
    public void findMax() {
        java.util.Map<String, String> map = new java.util.HashMap<>();
        map.put("client_id", "180100031051");
        map.put("product_id", "3654");
        map.put("width", "720");
        JSONObject json = find(map);
        if (json == null) {
            return;
        }
        long now = System.currentTimeMillis();
        JSONObject data = json.getJSONObject("data");
        log.info(data.getString("name"));
        JSONArray color = data.getJSONArray("commodity_list");
        for (int i = 0; i < color.length(); i++) {
            msg.setLength(0);
            JSONObject c = color.getJSONObject(i);
            if (c.getBoolean("is_cos")) {
                msg.append(c.getString("icon_name"));
                msg.append("-->没货");
            } else {
                msg.append(data.getString("name"));
                msg.append(c.getString("icon_name"));
                msg.append("-->有货！！！  ");
                // 离上次通知大于3分钟就短信通知
                if (now - lastTime_max > 3L * 60 * 1000 && count_max < 3) {
                    lastTime_max = now;
                    count_max++;
                    send(msg.toString());
                }
            }
            log.info(msg.toString());
        }
        log.info("================================================");
    }

    /**
     * 获取尊享版小米5当前状态
     */
    public void findMi5() {
        java.util.Map<String, String> map = new java.util.HashMap<>();
        map.put("client_id", "180100031051");
        map.put("product_id", "3281");
        map.put("width", "720");
        JSONObject json = find(map);
        if (json == null) {
            return;
        }
        long now = System.currentTimeMillis();
        JSONObject data = json.getJSONObject("data");
        log.info(data.getString("name"));
        JSONArray color = data.getJSONArray("commodity_list");
        for (int i = 0; i < color.length(); i++) {
            msg.setLength(0);
            JSONObject c = color.getJSONObject(i);
            if (c.getBoolean("is_cos")) {
                msg.append(c.getString("icon_name"));
                msg.append("-->没货");
            } else {
                msg.append(data.getString("name"));
                msg.append(c.getString("icon_name"));
                msg.append("-->有货！！！  ");
                // 离上次通知大于3分钟就短信通知
                if (now - lastTime_mi5 > 3L * 60 * 1000 && count_mi5 < 3) {
                    lastTime_mi5 = now;
                    count_mi5++;
                    send(msg.toString());
                }
            }
            log.info(msg.toString());
        }
        log.info("================================================");
    }

    public void send(String msg) {
        PushApiManager.pushMessageToAll(msg);
    }

    private JSONObject find(java.util.Map<String, String> data) {
        JSONObject reValue = null;
        String str = "http://m.mi.com/v1/product/view";
        Connection conn = Jsoup.connect(str);
        try {
            conn.timeout(5000); //30秒超时
            conn.method(Method.POST);
            conn.ignoreContentType(true);
            conn.userAgent(
                    "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36");
            conn.referrer("http://m.mi.com/1/");
            conn.data(data);
            Response resp = conn.execute();
            // 成功获取当前销售状态
            if (resp.statusCode() == 200 && resp.statusMessage().equalsIgnoreCase("ok")) {
                reValue = new JSONObject(resp.body());
            } else {
                log.warn("状态码:" + resp.statusCode() + ",状态值:" + resp.statusMessage());
            }
            error = 0;
        } catch (java.io.IOException e) {
            log.error(e);
//			error++;
//			if (error >=3) {
//				loop = false;
//			}
        } catch (JSONException e) {
            log.error(e);
//			error++;
//			if (error >=3) {
//				loop = false;
//			}
        }
        return reValue;
    }
}
